package protocol;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;

import protocol.meta.APPSetting;
import protocol.meta.BindingAccountVO;
import protocol.meta.PageParam;
import protocol.meta.User;
import protocol.transactions.TransactionUpload.UploadPrama;
import Utils.XoneUtil;
import android.content.Context;
import android.text.TextUtils;
import app.BaseApplication;

import com.netease.http.THttpMethod;
import com.netease.http.THttpRequest;
import com.netease.http.Entities.FilePart;
import com.netease.http.Entities.MultipartEntity;
import com.netease.http.Entities.Part;
import com.netease.http.Entities.StringPart;
import com.netease.log.NTLog;
import com.netease.pkgRelated.XoneConsts;
import com.netease.util.AndroidUtil;
import com.netease.util.EnctryUtil;
import com.netease.util.PlatformUtil;

public class XoneProtocol
{
    //服务器域名
//    	private String SERVER_DOMAIN = "http://x1.163.com/";//线上,切的时候切记同时修改统计"MA-A1C4-5575F8515F7E"
//    private String SERVER_DOMAIN = "http://192.168.144.151:8080/";//李辉机器
    private String SERVER_DOMAIN = "http://test.x1.163.com/";//测试

    private final static String sUserAgent = "NETS_Android";
    private final static int sTimeout = 10 * 1000;
    private final static int PUSH_TIME_OUT = 11 * 60 * 1000;//推送协议的超时时间

    //反馈产品ID
    public static String PRODUCT_ID = "23001";
    //反馈产品ID,异常跟踪反馈，供开发跟踪crash等问题使用
    public static String PRODUCT_ID_DEBUG = "23002";
    //反馈类型ID, APP反馈
    public static String FEEDBACK_TYPE_ID_APP = "29005";
    //反馈类型ID, 资讯反馈
    public static String FEEDBACK_TYPE_ID_INFO = "32003";
    //反馈类型ID, Crash反馈
    public static String FEEDBACK_TYPE_ID_CRASH = "29003";

    private static String ZIP_LOG_FILE_NAME = "xone_android_log.zip";

    public static String XONE_PRODUCT_ID = "xone";
    private final static int CheckVersion_AndroidPhone_Type = 3;
    public static String XONE_PROTOCOL_NUMBER = "1.0.0";

    /*************************************************
     * API
     */
    /**
     * 登录注册
     */
    //获取ID和密钥
    private static final String URL_GET_IDKEY = "login/id_key_get.json";
    //登录
    private static final String URL_LOGIN = "login/login.json";
    //检查手机号唯一性
    private static final String URL_CHECK_PHONE_NUMBER = "login/phonenumber_check.json";
    //  urs登录的用户当昵称有重复时的重新注册
    private static final String URL_URS_USER_REGISTER = "login/urs_register.json";
    //  第三方用户注册
    private static final String URL_EXTERNAL_USER_REGISTER = "login/third_register.json";
    //  获取手机验证码
    private static final String  URL_GET_CELLPHONE_CAPTCHA_REG = "login/getMobileCode.json";
    //  获取手机验证码（修改密码）
    private static final String  URL_GET_CELLPHONE_CAPTCHA_FINDPW = "login/getMobilePasswordCode.json";
    //  手机号码注册
    private static final String  URL_CELLPHONE_REG = "login/mobile_register.json";
    //  修改手机登录密码
    private static final String  URL_MODIFY_CELLPHONE_PW = "login/updateMobilePassword.json";
    //   注销帐号
    private static final String  URL_LOGOUT = "login/switch_logout.json";
    /**
     * 设置
     */
    // 获得帐号绑定信息
    private static final String URL_GET_BINDING_INFO = "set/get_bindingInfo.json";
    // 上传帐号绑定信息
    private static final String URL_UPLOAD_BINDING_INFO = "set/uploadBindingAccount.json";
    // 获取应用参数
    private static final String URL_GET_APP_SETTING = "set/app_get.json";
    // 设置应用参数
    private static final String URL_SET_APP_SETTING = "set/app_set.json";
    //推荐APP
    private static final String URL_RECOMM_APP = "http://app.zs.163.com/dma/android/phone/opencourse/app.json";
    //上传日志
    private static final String URL_POST_LOG = "http://fankui.163.com/ft/file.fb?op=up";
    //反馈
    private static final String URL_FEEDBACK = "http://fankui.163.com/ft/commentInner.fb?";
    //检查版本号
    private static final String URL_CHECK_APP_VERSOIN = "app/getAppInfo.json";
    /**
     * 设置end
     */
    // 获取资讯头条推荐
    private static final String URL_HEAD_RECOMMEND = "info/getHeadRecommend.json";
    // 获取输入提示关注用户列表
    private static final String URL_USERINPUTTIPLIST = "info/getUserInputTipList.json";
    // 获取输入提示关注用户列表
    private static final String URL_SOURCEINPUTTIPLIST = "info/getSourceInputTipList.json";
    // 获取输入提示订阅资讯源列表
    private static final String URL_TOPICINPUTTIPLIST = "info/getTopicInputTipList.json";
    // 获取输入提示热门话题列表
    private static final String URL_SUBJECTINPUTTIPLIST = "info/getSubjectInputTipList.json";
    // 图片，视频，音频上传
    private static final String URL_UPLOAD = "info/upload.json";
    // 发表讨论
    private static final String URL_POSTDISCUSS = "info/postDiscuss.json";
    // 批量获取资讯ID
    private static final String URL_GET_INFOIDS = "info/getInfoIds.json";
    // 获取资讯列表
    private static final String URL_GET_INFOLIST = "info/getInfoList.json";
    // 获取资讯正文
    private static final String URL_GET_INFODETAIL = "info/getInfoDetail.json";
    // 获取资讯评论，转发，赞
    private static final String URL_GET_INFOCFP = "info/getInfoCfp.json";
    // 评论资讯
    private static final String URL_COMMENTINFO = "info/commentInfo.json";
    // 转发资讯
    private static final String URL_FORWARDINFO = "info/forwardInfo.json";
    // 获取未读资讯条数
    private static final String URL_UNREAD_INFOCOUNT = "info/getUnReadInfoCount.json";
    // 赞资讯
    private static final String URL_PRAISEINFO = "info/praiseInfo.json";
    // （订阅）获取游戏、综合类主题类表
    private static final String URL_GET_SUBJECT_LIET = "source/listSubject.json";
    // 搜索主题和资讯源
    private static final String URL_SEARCH_SOURCE_AND_SUBJECT = "source/search.json";
    // 获取最新资讯源列表
    private static final String URL_GET_LATEST_SOURCE_LIST = "source/getLatestSourceList.json";
    // 获取最热主题列表
    private static final String URL_GET_HOTEST_SUBJECT_LIST = "source/getHotestSubjectList.json";
    // 获取推荐主题列表
    private static final String URL_GET_RECOMMEND_SUBJECT_LIST = "source/getRecommendSubjectList.json";
    // 获取主题信息（含主题资讯源数据）
    private static final String URL_GET_SUBJECT_DETAIL = "source/getSubjectDetail.json";
    // 获取资讯源信息
    private static final String URL_GET_SOURCE_DETAIL = "source/getSourceDetail.json";
    // 订阅
    private static final String URL_SUBSCRIBE = "source/subscribe.json";
    // 取消订阅
    private static final String URL_UNSUBSCRIBE = "source/unsubscribe.json";
    // 获取资讯源下资讯列表
    private static final String URL_GET_SOURCE_INFO_LIST = "source/getSourceInfoList.json";
    // 获取用户订阅的主题
    private static final String URL_GET_SUBSCRIBE_SUBJECT = "user/getSubscribeList.json";
    // 找朋友-人气榜
    private static final String URL_GET_POP_FRIEND_LIST = "square/friend_pop_list.json";
    // 找朋友-猜你喜欢
    private static final String URL_GET_GUESS_FRIEND_LIST = "square/friend_guess_list.json";
    // 发现讨论-最热讨论
    private static final String URL_GET_POP_DISCUSS_LIST = "square/discuss_list.json";
    // 发现讨论-猜你喜欢
    private static final String URL_GET_GUESS_DISCUSS_LIST = "square/guess_list.json";
    // 获取热门话题
    private static final String URL_GET_TOPIC_LIST = "square/topic_list.json";
    // 获取用户信息
    private static final String URL_GET_USER_DETAIL = "user/getUserDetail.json";
    // 获取用户粉丝列表
    private static final String URL_GET_FANS_LIST = "user/getFansList.json";
    // 获取用户关注列表
    private static final String URL_GET_FOLLOW_LIST = "user/getFollowList.json";
    // 关注
    private static final String URL_FOLLOW = "user/follow.json";
    // 取消关注
    private static final String URL_CANCEL_FOLLOW = "user/cancelFollow.json";
    // 修改用户信息
    private static final String URL_UPDATE_USER = "user/updateUser.json";
    // 获取好友动态信息列表
    private static final String URL_GET_FRIEND_DYNAMIC_LIST = "message/friend_dynamic_list.json";
    // 获取好友动态信息ids
    private static final String URL_GET_DYNAMIC_IDS = "message/dynamic_ids.json";
    // 获取通知列表
    private static final String URL_GET_ADVICE_LIST = "message/advice_list.json";
    // 反馈官方资讯
    private static final String URL_FEED_BACK = "info/feedback.json";
    // 举报用户讨论
    private static final String URL_REPORT = "info/report.json";
    // 获取主题下讨论
    private static final String URL_GET_SUBJECT_INFO_LIST = "source/getSubjectDiscussList.json";
    // 获取话题下讨论
    private static final String URL_GET_TOPIC_INFO_LIST = "square/topic_info_list.json";
    // 获取被评论的讨论
    private static final String URL_GET_COMMENT_DISCUSS_LIST = "message/comment_discuss_list.json";
    // 获取@我的讨论
    private static final String URL_GET_AT_DISCUSS_LIST = "message/at_discuss_list.json";
    // 获取赞我的讨论
    private static final String URL_GET_PRAISE_DISCUSS_LIST = "message/praised_list.json";
    // 获取赞我的讨论
    private static final String URL_GET_USER_DISCUSS_LIST = "user/getDiscussList.json";

    private static XoneProtocol mInstance;
    //当前登录帐号的cookie
    private static  String AUTHORIZED_COOKIE = null;
    //应用服务器生成的Id（跟设备对应，与用户无关，每次应用启动获取一次）
    private   String initId = null;
    //应用服务器生成的秘钥，密钥使用16进制的字符串表示，（跟设备对应，与用户无关，每次应用启动获取一次）
    private   String encryptedKey = null;


    XoneProtocol()
    {
    }



    public static XoneProtocol getInstance()
    {
        if (mInstance == null)
        {
            mInstance = new XoneProtocol();
        }
        return mInstance;
    }

    /**
     *
     * @return
     */
    public String getSever_Domain() {
        return SERVER_DOMAIN;
    }


    /**
     * 获取完整请求URL
     * @param url
     * @return
     */
    private String getRequestUrl(String url) {
        return SERVER_DOMAIN + url;
    }

    public static String getCookie() {
        return AUTHORIZED_COOKIE;
    }
    /**
     * 设置当前登录帐号的cookie
     * @param cookie
     */
    public static void setCookie(String cookie) {
        AUTHORIZED_COOKIE = cookie;
    }
    public  void setIntitId(String iId) {
        initId = iId;
    }
    public  String getIntitId() {
        return initId;
    }
    public  void setEncryptedKey(String key) {
        encryptedKey = key;
    }
    public  String getEncryptedKey() {
        return encryptedKey;
    }
    /**
     * 获取ID和密钥
     * @return
     */
    public THttpRequest createGegIdKeyRequest(){
        Context context = BaseApplication.getAppInstance().getApplicationContext();
        String pdtVersion = XoneUtil.getNumberVersion(context);
        String deviceId = PlatformUtil.getDeviceID(context);
        String deviceType = PlatformUtil.getMobileName();
        String systemVersion = PlatformUtil.getOSVersion();
        String resolution = PlatformUtil.getScreenResolution(context);
        String appChannel = XoneUtil.getAppChannelID(context);

        XoneHttpRequest request = new XoneHttpRequest(getRequestUrl(URL_GET_IDKEY),THttpMethod.GET);
        request.addParameter("product", XONE_PRODUCT_ID);
        request.addParameter("pdtVersion", pdtVersion);
        request.addParameter("deviceId", deviceId);
        request.addParameter("deviceType", deviceType);
        request.addParameter("systemName", "Android");
        request.addParameter("systemVersion", systemVersion);
        request.addParameter("resolution", resolution);
        request.addParameter("appChannel", appChannel);
        return request;
    }

    /**
     * 登录协议
     * @return
     */
    public THttpRequest createLoginRequest(int type,String username, String pwd){
        if (type == XoneConsts.ACCOUNT_TYPE.Urs || type == XoneConsts.ACCOUNT_TYPE.Phone) {
            pwd = EnctryUtil.getMd5Hash(pwd);
        }
        String encrypt = null;
        if (type != XoneConsts.ACCOUNT_TYPE.Anonymous) {
            encrypt = "type=" + type + "&username=" + username + "&password=" + pwd;
        } else {
            encrypt = "type=" + type;
        }
        try {
            encrypt = EnctryUtil.encryptForAES(encrypt, encryptedKey);
        } catch (Exception e) {
            e.printStackTrace();
        }
        XoneHttpRequest request = new XoneHttpRequest(getRequestUrl(URL_LOGIN), THttpMethod.GET);
        request.addParameter("initId", initId);
        request.addParameter("encrypt", encrypt);
        return request;
    }
    /**
     * 检查手机号唯一性
     * @return
     */
    public THttpRequest createCheckPhoneNumberRequest(String pnumber){
        XoneHttpRequest request = new XoneHttpRequest(getRequestUrl(URL_CHECK_PHONE_NUMBER),THttpMethod.GET);
        request.addParameter("phoneNumber", pnumber);
        return request;
    }

    /**
     * 获取资讯头条推荐
     * @return
     */
    public THttpRequest createHeadRecommendRequest() {
        XoneHttpRequest request = new XoneHttpRequest(getRequestUrl(URL_HEAD_RECOMMEND),THttpMethod.GET);
        request.setCacheFile();
        request.setCacheDatabase();
        return request;
    }

    /**
     * 获取输入提示关注用户列表
     * @return
     */
    public THttpRequest createUserInputTipListRequest(String type) {
        XoneHttpRequest request = new XoneHttpRequest(getRequestUrl(URL_USERINPUTTIPLIST),THttpMethod.GET);
        request.addParameter("type", type);
        return request;
    }

    /**
     * 获取输入提示订阅资讯源列表
     * @return
     */
    public THttpRequest createSourceInputTipListRequest(String type) {
        XoneHttpRequest request = new XoneHttpRequest(getRequestUrl(URL_SOURCEINPUTTIPLIST),THttpMethod.GET);
        request.addParameter("type", type);
        return request;
    }

    /**
     * 获取输入提示热门话题列表
     * @return
     */
    public THttpRequest createTopicInputTipListRequest() {
        XoneHttpRequest request = new XoneHttpRequest(getRequestUrl(URL_TOPICINPUTTIPLIST),THttpMethod.GET);
        return request;
    }

    /**
     * 获取输入提示订阅主题列表
     * @return
     */
    public THttpRequest createSubjectInputTipListRequest() {
        XoneHttpRequest request = new XoneHttpRequest(getRequestUrl(URL_SUBJECTINPUTTIPLIST),THttpMethod.GET);
        return request;
    }

    /**
     * 图片，视频，音频上传
     * @return
     */
    public THttpRequest createUploadRequest(UploadPrama prama) {
        XoneHttpRequest request = new XoneHttpRequest(getRequestUrl(URL_UPLOAD),THttpMethod.POST);

        do {
            if (null == prama) {
                break;
            }

            if (TextUtils.isEmpty(prama.path)) {
                break;
            }

            File file = new File(prama.path);
            if (!file.exists()) {
                break;
            }

            String fileName = file.getName();
            String ext = null;
            int index = fileName.lastIndexOf('.');
            if (index > 0) {
                ext = fileName.substring(index);
            }

            try {
                Part[] parts = null;
                FilePart filePart = new FilePart("data", file);

                if (UploadPrama.TYPE_AUDIO == prama.type) {
                    if (TextUtils.isEmpty(ext)) {
                        ext = ".3gp";
                    }
                    parts = new Part[] {
                            new StringPart("name", fileName),
                            new StringPart("type", String.valueOf(prama.type)),
                            new StringPart("ext", ext),
                            new StringPart("duration", String.valueOf(prama.duration)),
                            filePart
                    };
                } else {
                    if (TextUtils.isEmpty(ext)) {
                        ext = ".jpg";
                    }
                    parts = new Part[] {
                            new StringPart("name", fileName),
                            new StringPart("type", String.valueOf(prama.type)),
                            new StringPart("ext", ext),
                            filePart
                    };
                }

                request.setHttpEntity(new MultipartEntity(parts));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }

        } while (false);

        return request;
    }

    /**
     * 发表讨论
     * @param content
     * @param imageUrl
     * @param audioUrl
     * @return
     */
    public THttpRequest createPostDiscuss(String content, String imageUrl, UploadPrama audio) {
        XoneHttpRequest request = new XoneHttpRequest(getRequestUrl(URL_POSTDISCUSS),THttpMethod.POST);
        List<NameValuePair> list = new LinkedList<NameValuePair>();

        // 必填参数
        list.add(new BasicNameValuePair("text", content));

        if (!TextUtils.isEmpty(imageUrl)) {
            list.add(new BasicNameValuePair("imageUrl", imageUrl));
        }

        if (null != audio && !TextUtils.isEmpty(audio.path)) {
            list.add(new BasicNameValuePair("audioUrl", audio.path));
            list.add(new BasicNameValuePair("duration", String.valueOf(audio.duration)));
        }

        try {
            // 一定要设置编码方式为uff-8,不让中文会乱码
            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, "UTF-8");
            request.setHttpEntity(entity);
        } catch (Exception e) {
        }

        return request;
    }

    /**
     * 评论资讯
     * @param content
     * @param imageUrl
     * @param audio
     * @return
     */
    public THttpRequest createCommentInfo(String content, String imageUrl, UploadPrama audio) {
        XoneHttpRequest request = new XoneHttpRequest(getRequestUrl(URL_COMMENTINFO),THttpMethod.POST);
        List<NameValuePair> list = new LinkedList<NameValuePair>();

        // 必填参数
        list.add(new BasicNameValuePair("text", content));

        list.add(new BasicNameValuePair("infoId", audio.infoId));

        list.add(new BasicNameValuePair("flag", String.valueOf(audio.flag)));

        try {
            // 一定要设置编码方式为uff-8,不让中文会乱码
            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, "UTF-8");
            request.setHttpEntity(entity);
        } catch (Exception e) {
        }

        return request;
    }

    /**
     * 转发资讯
     * @param content
     * @param imageUrl
     * @param audio
     * @return
     */
    public THttpRequest createForwardInfo(String content, String imageUrl, UploadPrama audio) {
        XoneHttpRequest request = new XoneHttpRequest(getRequestUrl(URL_FORWARDINFO),THttpMethod.POST);
        List<NameValuePair> list = new LinkedList<NameValuePair>();

        // 必填参数
        list.add(new BasicNameValuePair("text", content));

        list.add(new BasicNameValuePair("infoId", audio.infoId));

        list.add(new BasicNameValuePair("flag", String.valueOf(audio.flag)));

        try {
            // 一定要设置编码方式为uff-8,不让中文会乱码
            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, "UTF-8");
            request.setHttpEntity(entity);
        } catch (Exception e) {
        }

        return request;
    }

    /**
     * urs登录的用户当昵称有重复时的重新注册
     * @return
     */
    public THttpRequest createUrsUserRegRequest(String username, String pwd, String nickname){
        pwd = EnctryUtil.getMd5Hash(pwd);
        String encrypt = "username=" + username + "&password=" + pwd + "&nickname=" + nickname;
        try {
            encrypt = EnctryUtil.encryptForAES(encrypt, encryptedKey);
        } catch (Exception e) {
            e.printStackTrace();
        }
        XoneHttpRequest request = new XoneHttpRequest(getRequestUrl(URL_URS_USER_REGISTER),THttpMethod.GET);
//        XoneHttpRequest request = new XoneHttpRequest("http://192.168.144.151/xone/login/urs_register.json",THttpMethod.GET);
        request.addParameter("initId", initId);
        request.addParameter("encrypt", encrypt);
        return request;
    }

    /**
     * （订阅）获取游戏、综合类的主题列表
     * @return
     */
    public THttpRequest createGetSubjectListRequest(int type) {
        XoneHttpRequest request = new XoneHttpRequest(getRequestUrl(URL_GET_SUBJECT_LIET),THttpMethod.GET);
        request.addParameter("type", String.valueOf(type));
        return request;
    }
    /**
     * 第三方用户注册
     * @return
     */
    public THttpRequest createExternalUserRegRequest(String uid,String accessToken,int type,String nickname,String avatar){
        String encrypt = null;
        if(!TextUtils.isEmpty(avatar)) {
            encrypt = "uid=" + uid + "&accessToken=" + accessToken + "&type=" + type + "&nickname=" + nickname + "&avatar=" + avatar;
        } else {
            encrypt = "uid=" + uid + "&accessToken=" + accessToken + "&type=" + type + "&nickname=" + nickname;
        }
        try {
            encrypt = EnctryUtil.encryptForAES(encrypt, encryptedKey);
        } catch (Exception e) {
            e.printStackTrace();
        }
        XoneHttpRequest request = new XoneHttpRequest(getRequestUrl(URL_EXTERNAL_USER_REGISTER),THttpMethod.GET);
        request.addParameter("initId", initId);
        request.addParameter("encrypt", encrypt);
        return request;
    }
    /**
     * 获取手机验证码
     * @return
     */
    public THttpRequest createGetCaptcha(int getType,String phoneNumber){
        String encrypt = "phoneNumber=" + phoneNumber;
        try {
            encrypt = EnctryUtil.encryptForAES(encrypt, encryptedKey);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String url = null;
        if(getType == XoneConsts.INPUT_CELLPHONE_TYPE.Register) {
            url = getRequestUrl(URL_GET_CELLPHONE_CAPTCHA_REG);
        } else {
            url = getRequestUrl(URL_GET_CELLPHONE_CAPTCHA_FINDPW);
        }
        XoneHttpRequest request = new XoneHttpRequest(url,THttpMethod.GET);
        request.addParameter("initId", initId);
        request.addParameter("encrypt", encrypt);
        return request;
    }
    /**
     * urs登录的用户当昵称有重复时的重新注册
     * @return
     */
    public THttpRequest createCellPhoneRegRequest(String phoneNumber,String nickName,String captcha,String pw){
        pw = EnctryUtil.getMd5Hash(pw);
        String encrypt = "phoneNumber=" + phoneNumber + "&nickname=" + nickName + "&verifyCode=" + captcha + "&password=" + pw;
        try {
            encrypt = EnctryUtil.encryptForAES(encrypt, encryptedKey);
        } catch (Exception e) {
            e.printStackTrace();
        }
        XoneHttpRequest request = new XoneHttpRequest(getRequestUrl(URL_CELLPHONE_REG),THttpMethod.GET);
        request.addParameter("initId", initId);
        request.addParameter("encrypt", encrypt);
        return request;
    }
    /**
     *  修改手机登录密码
     * @return
     */
    public THttpRequest createModifyCellPhonPwRequest(String phoneNumber,String captcha,String pw){
        pw = EnctryUtil.getMd5Hash(pw);
        String encrypt = "phoneNumber=" + phoneNumber + "&verifyCode=" + captcha + "&password=" + pw;
        try {
            encrypt = EnctryUtil.encryptForAES(encrypt, encryptedKey);
        } catch (Exception e) {
            e.printStackTrace();
        }
        XoneHttpRequest request = new XoneHttpRequest(getRequestUrl(URL_MODIFY_CELLPHONE_PW),THttpMethod.GET);
        request.addParameter("initId", initId);
        request.addParameter("encrypt", encrypt);
        return request;
    }
    /**
     * 注销帐号
     * @return
     */
    public THttpRequest createLogoutRequest(){
        XoneHttpRequest request = new XoneHttpRequest(getRequestUrl(URL_LOGOUT),THttpMethod.GET);
        request.addParameter("initId", initId);
        return request;
    }

    /**
     * 搜索
     * @return
     */
    public THttpRequest createSearchRequest(int type, String queryString) {
        XoneHttpRequest request = new XoneHttpRequest(getRequestUrl(URL_SEARCH_SOURCE_AND_SUBJECT),THttpMethod.GET);
        request.addParameter("keyword", queryString);
        return request;
    }

    /**
     * 获得最新资讯源列表
     * @return
     */
    public THttpRequest createGetLatestSourceListRequest() {
        XoneHttpRequest request = new XoneHttpRequest(getRequestUrl(URL_GET_LATEST_SOURCE_LIST),THttpMethod.GET);
        return request;
    }

    /**
     * 获得最热主题列表
     * @return
     */
    public THttpRequest createGetHotestSubjectListRequest() {
        XoneHttpRequest request = new XoneHttpRequest(getRequestUrl(URL_GET_HOTEST_SUBJECT_LIST),THttpMethod.GET);
        return request;
    }

    /**
     * 获得推荐主题列表
     * @return
     */
    public THttpRequest createGetRecommendSubjectListRequest() {
        XoneHttpRequest request = new XoneHttpRequest(getRequestUrl(URL_GET_RECOMMEND_SUBJECT_LIST),THttpMethod.GET);
        return request;
    }
    /**
     * 获得帐号绑定信息
     * @return
     */
    public THttpRequest createGetBindingInfoRequest() {
        XoneHttpRequest request = new XoneHttpRequest(getRequestUrl(URL_GET_BINDING_INFO),THttpMethod.GET);
        return request;
    }
    /**
     * 上传绑定帐号
     * @return
     */
    public THttpRequest createUploadBindingInfoRequest( List<BindingAccountVO> accList) {
        if(accList == null || accList.size() == 0 ) {
            return null;
        }
        JSONArray list = new JSONArray();
        for(int i=0;i<accList.size();i++){
            list.put(accList.get(i).getJSONObject());
        }
        XoneHttpRequest request = new XoneHttpRequest(getRequestUrl(URL_UPLOAD_BINDING_INFO),THttpMethod.POST);
        List<NameValuePair> dataList = new LinkedList<NameValuePair>();
        try {
            dataList.add(new BasicNameValuePair("accountlist", list.toString()));
            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(dataList,"UTF-8");
            request.setHttpEntity(entity);
        } catch (Exception e) {
        }
        return request;
    }
    /**
     * 批量获取资讯ID
     * @param infoGroup
     * @param sortType
     * @param attach
     * @return
     */
    public THttpRequest createGetInfoIdsRequest(int infoGroup, int sortType, int attach) {
        XoneHttpRequest request = new XoneHttpRequest(getRequestUrl(URL_GET_INFOIDS),THttpMethod.GET);
        if (infoGroup >= 0) {
            request.addParameter("infoGroup", String.valueOf(infoGroup));
        }

        if (sortType > 0) {
            request.addParameter("sortType", String.valueOf(sortType));
        }

        if (attach > 0) {
            request.addParameter("attach", String.valueOf(attach));
        }

        return request;
    }

    /**
     * 获取资讯列表
     * @param infoIds
     * @return
     */
    public THttpRequest createGetInfoList(String infoIds) {
        XoneHttpRequest request = null;
        request = new XoneHttpRequest(getRequestUrl(URL_GET_INFOLIST),THttpMethod.GET);
        request.addParameter("infoId",infoIds);
        return request;
    }

    /**
     * 获取主题详情
     * @param subjectId
     * @param subjectName
     * @return
     */
    public THttpRequest createGetSubjectDetailRequest(int subjectId, String subjectName) {
        XoneHttpRequest request = new XoneHttpRequest(getRequestUrl(URL_GET_SUBJECT_DETAIL), THttpMethod.GET);
        if (subjectId > 0) {
            request.addParameter("subjectId", String.valueOf(subjectId));
        }
        if (!TextUtils.isEmpty(subjectName)) {
            request.addParameter("subjectName", subjectName);
        }
        return request;
    }
    /**
     * 反馈接口:反馈log
     *
     * @param
     * @return
     */
    public THttpRequest createPostLogReqeust()
    {
        THttpRequest request = new THttpRequest(URL_POST_LOG, THttpMethod.POST);
        File upload = null;
        boolean isZip = NTLog.zipLogFile(ZIP_LOG_FILE_NAME);
        if(isZip){
            upload = NTLog.openAbsoluteFile(ZIP_LOG_FILE_NAME);
            NTLog.i("createPostLogReqeust upload=", upload.toString());
        } else {
            NTLog.i("createPostLogReqeust()", "zip fail");
        }

        Part part1 = null;
        try {
            part1 = new FilePart("Filedata",ZIP_LOG_FILE_NAME, upload,"application/zip",null);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        MultipartEntity entity = new MultipartEntity(new Part[]{part1});
        request.setHttpEntity(entity);
        return request;

    }
    /**
     * 反馈接口:反馈意见
     *
     * @param
     * @return
     */
    public THttpRequest createFeedBackReqeust(String pid,String fid,String user,String title,String content,String fileId,String contact,Context context){
        THttpRequest request = new THttpRequest(URL_FEEDBACK, THttpMethod.POST);

        request.addHeader("Content-Type", "application/x-www-form-urlencoded");
        request.addHeader("User-Agent",XoneHttpRequest.USERAGENT);

        String resolution = AndroidUtil.getResolution(context) + ";" + android.os.Build.MODEL + "/android" + android.os.Build.VERSION.RELEASE  + ";" + XoneConsts.APP_OS_NAME + "/" + XoneUtil.getNumberVersion(context);


        List<NameValuePair> list = new ArrayList<NameValuePair>();
        list.add(new BasicNameValuePair("feedbackId", fid));
        list.add(new BasicNameValuePair("productId", pid));
        list.add(new BasicNameValuePair("userName", user));
        list.add(new BasicNameValuePair("title", title));
        list.add(new BasicNameValuePair("content", content));
        list.add(new BasicNameValuePair("resolution", resolution));
        if (fileId != null) {
            list.add(new BasicNameValuePair("fileId", fileId));
            list.add(new BasicNameValuePair("fileName", ZIP_LOG_FILE_NAME));
        }
        if(contact != null){
            list.add(new BasicNameValuePair("contact", contact));
        }

        try {
            request.setHttpEntity(new UrlEncodedFormEntity(list, "GB2312"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        return request;


    }

    /**
     * 获取资讯源详情
     * @param subjectId
     * @param subjectName
     * @return
     */
    public THttpRequest createGetSourceDetailRequest(int sourceId) {
        XoneHttpRequest request = new XoneHttpRequest(getRequestUrl(URL_GET_SOURCE_DETAIL), THttpMethod.GET);
        request.addParameter("sourceId", String.valueOf(sourceId));
        return request;
    }

    /**
     * 获取资讯正文
     * @param infoId
     * @return
     */
    public THttpRequest createGetInfoDetailRequest(String infoId) {
        XoneHttpRequest request = new XoneHttpRequest(getRequestUrl(URL_GET_INFODETAIL), THttpMethod.GET);
        request.addParameter("infoId", infoId);
        return request;
    }

    /**
     * 获取应用参数
     * @return
     */
    public THttpRequest createGetAppSettingRequest() {
        XoneHttpRequest request = new XoneHttpRequest(getRequestUrl(URL_GET_APP_SETTING), THttpMethod.GET);
        return request;
    }
    /**
     * 设置应用参数
     * @return
     */
    public THttpRequest createSetAppSettingRequest(APPSetting setting) {
        if(setting == null){
            return null;
        }
        XoneHttpRequest request = new XoneHttpRequest(getRequestUrl(URL_SET_APP_SETTING), THttpMethod.POST);
        List<NameValuePair> dataList = new LinkedList<NameValuePair>();
        try {
            dataList.add(new BasicNameValuePair("config", setting.toString()));
            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(dataList);
            request.setHttpEntity(entity);
        } catch (Exception e) {
        }
        return request;
    }

    /**
     * 订阅
     * @return
     */
    public THttpRequest createSubscribelRequest(int subjectId, int sourceId) {
        XoneHttpRequest request = new XoneHttpRequest(getRequestUrl(URL_SUBSCRIBE), THttpMethod.GET);
        if(subjectId > 0){
            request.addParameter("subjectId", String.valueOf(subjectId));
        }
        if(sourceId > 0){
            request.addParameter("sourceId", String.valueOf(sourceId));
        }
        return request;
    }

    /**
     * 取消订阅
     * @return
     */
    public THttpRequest createUnsubscribelRequest(int sourceId) {
        XoneHttpRequest request = new XoneHttpRequest(getRequestUrl(URL_UNSUBSCRIBE), THttpMethod.GET);
        request.addParameter("sourceId", String.valueOf(sourceId));
        return request;
    }

    /**
     * 获取资讯评论，转发，赞
     * @param infoId
     * @param type
     * @param param
     * @return
     */
    public THttpRequest createGetInfoCfp(String infoId, int type, PageParam param) {
        XoneHttpRequest request = new XoneHttpRequest(getRequestUrl(URL_GET_INFOCFP), THttpMethod.GET);
        request.addParameter("infoId", infoId);
        request.addParameter("type", String.valueOf(type));
        request.addParameter("count", String.valueOf(param.count));
        return request;
    }


    /**
     * 获取用户订阅的主题
     * @return
     */
    public THttpRequest createGetSubscribeListRequest(int userId) {
        XoneHttpRequest request = new XoneHttpRequest(getRequestUrl(URL_GET_SUBSCRIBE_SUBJECT), THttpMethod.GET);
        request.addParameter("sourceId", String.valueOf(userId));
        return request;
    }

    /**
     * 获取推荐应用列表
     * @return
     */
    public THttpRequest createGetRecommAppRequest() {
        XoneHttpRequest request = new XoneHttpRequest(URL_RECOMM_APP, THttpMethod.GET);
        return request;
    }
    /**
     * 获取推荐应用列表
     * @return
     */
    public THttpRequest createCheckAppVersion() {
        XoneHttpRequest request = new XoneHttpRequest(getRequestUrl(URL_CHECK_APP_VERSOIN), THttpMethod.GET);
        request.addParameter("channel", XoneUtil.getAppChannelID(BaseApplication.getAppInstance()));
        request.addParameter("apptype", String.valueOf(CheckVersion_AndroidPhone_Type));
        request.addParameter("version", XoneUtil.getNumberVersion(BaseApplication.getAppInstance()));
        return request;
    }

    /**
     * 获取未读资讯条数
     * @return
     */
    public THttpRequest createUnReadInfoCount() {
        XoneHttpRequest request = new XoneHttpRequest(getRequestUrl(URL_UNREAD_INFOCOUNT),THttpMethod.GET);
        return request;
    }

    /**
     * 获取人气榜用户
     * @return
     */
    public THttpRequest createGetPopFriendListRequest(int currentPage, int pageSize) {
        XoneHttpRequest request = new XoneHttpRequest(getRequestUrl(URL_GET_POP_FRIEND_LIST),THttpMethod.GET);
        request.addParameter("currentPage", String.valueOf(currentPage));
        request.addParameter("pageSize", String.valueOf(pageSize));
        return request;
    }

    /**
     * 获取猜你喜欢用户
     * @return
     */
    public THttpRequest createGetGuessFriendListRequest(int currentPage, int pageSize) {
        XoneHttpRequest request = new XoneHttpRequest(getRequestUrl(URL_GET_GUESS_FRIEND_LIST),THttpMethod.GET);
        request.addParameter("currentPage", String.valueOf(currentPage));
        request.addParameter("pageSize", String.valueOf(pageSize));
        return request;
    }

    /**
     * 发现讨论-最热
     * @return
     */
    public THttpRequest createGetPopDiscussListRequest(int currentPage, int pageSize) {
        XoneHttpRequest request = new XoneHttpRequest(getRequestUrl(URL_GET_POP_DISCUSS_LIST),THttpMethod.GET);
        request.addParameter("currentPage", String.valueOf(currentPage));
        request.addParameter("pageSize", String.valueOf(pageSize));
        return request;
    }
    /**
     * 发现讨论-最热
     * @return
     */
    public THttpRequest createGetGuessDiscussListRequest(int currentPage, int pageSize) {
        XoneHttpRequest request = new XoneHttpRequest(getRequestUrl(URL_GET_GUESS_DISCUSS_LIST),THttpMethod.GET);
        request.addParameter("currentPage", String.valueOf(currentPage));
        request.addParameter("pageSize", String.valueOf(pageSize));
        return request;
    }

    /**
     * 赞资讯
     * @param infoId
     * @param praiseFlag
     * @return
     */
    public THttpRequest createPraiseInfoRequest(String infoId, boolean praiseFlag) {
        XoneHttpRequest request = new XoneHttpRequest(getRequestUrl(URL_PRAISEINFO),THttpMethod.GET);
        request.addParameter("infoId", infoId);
        request.addParameter("flag", String.valueOf(praiseFlag));
        return request;
    }

    /**
     * 获取热门话题
     * @return
     */
    public THttpRequest createGetTopicListRequest(int count) {
        XoneHttpRequest request = new XoneHttpRequest(getRequestUrl(URL_GET_TOPIC_LIST),THttpMethod.GET);
        request.addParameter("count", String.valueOf(count));
        return request;
    }

    public THttpRequest createGetUserDetailRequest(String userId) {
        XoneHttpRequest request = new XoneHttpRequest(getRequestUrl(URL_GET_USER_DETAIL), THttpMethod.GET);
        request.addParameter("userId", userId);
        return request;
    }

    /**
     * 获取用户粉丝列表
     * @return
     */
    public THttpRequest createGetFansListRequest(String userId, int count) {
        XoneHttpRequest request = new XoneHttpRequest(getRequestUrl(URL_GET_FANS_LIST),THttpMethod.GET);
        request.addParameter("userId", userId);
        if(count > 0){
            request.addParameter("count", String.valueOf(count));
        }
        return request;
    }

    /**
     * 获取用户关注列表
     * @return
     */
    public THttpRequest createGetFollowListRequest(String userId, int count) {
        XoneHttpRequest request = new XoneHttpRequest(getRequestUrl(URL_GET_FOLLOW_LIST),THttpMethod.GET);
        request.addParameter("userId", userId);
        if(count > 0){
            request.addParameter("count", String.valueOf(count));
        }
        return request;
    }

    /**
     * 关注
     * @param userId
     * @return
     */
    public THttpRequest createFollowRequest(String userId) {
        XoneHttpRequest request = new XoneHttpRequest(getRequestUrl(URL_FOLLOW),THttpMethod.GET);
        request.addParameter("userId", userId);
        return request;
    }

    /**
     * 取消关注
     * @param userId
     * @return
     */
    public THttpRequest createCancelFollowRequest(String userId) {
        XoneHttpRequest request = new XoneHttpRequest(getRequestUrl(URL_CANCEL_FOLLOW),THttpMethod.GET);
        request.addParameter("userId", userId);
        return request;
    }

    /**
     * 获取资讯源下资讯列表
     * @param sourceId
     * @return
     */
    public THttpRequest createGetSourceInfoList(String sourceId, int type, int count, long maxTime) {
        XoneHttpRequest request = new XoneHttpRequest(getRequestUrl(URL_GET_SOURCE_INFO_LIST),THttpMethod.GET);
        request.addParameter("sourceId", sourceId);
        request.addParameter("type", String.valueOf(type));
        if(type == 0){
            if(count > 0){
                request.addParameter("count", String.valueOf(count));
            }
            if(maxTime > 0){
                request.addParameter("maxTime", String.valueOf(maxTime));
            }
        }
        return request;
    }

    /**
     * 获取被评论过的讨论列表
     * @param count
     * @param maxTime
     * @param sinceTime
     * @return
     */
    public THttpRequest createGetCommentDiscussList(int count, long maxTime, long sinceTime) {
        XoneHttpRequest request = new XoneHttpRequest(getRequestUrl(URL_GET_COMMENT_DISCUSS_LIST),THttpMethod.GET);
        if(count > 0){
            request.addParameter("count", String.valueOf(count));
        }
        if(maxTime > 0){
            request.addParameter("maxTime", String.valueOf(maxTime));
        }
        if(sinceTime > 0){
            request.addParameter("sinceTime", String.valueOf(sinceTime));
        }
        return request;
    }

    /**
     * 获取@我的讨论列表
     * @param count
     * @param maxTime
     * @param sinceTime
     * @return
     */
    public THttpRequest createGetAtDiscussList(int type, int count, long maxTime, long sinceTime) {
        XoneHttpRequest request = new XoneHttpRequest(getRequestUrl(URL_GET_AT_DISCUSS_LIST),THttpMethod.GET);
        request.addParameter("type", String.valueOf(type));
        if(count > 0){
            request.addParameter("count", String.valueOf(count));
        }
        if(maxTime > 0){
            request.addParameter("maxTime", String.valueOf(maxTime));
        }
        if(sinceTime > 0){
            request.addParameter("sinceTime", String.valueOf(sinceTime));
        }
        return request;
    }

    /**
     * 获取赞我的讨论列表
     * @param count
     * @param maxTime
     * @param sinceTime
     * @return
     */
    public THttpRequest createGetPraiseDiscussList(int count, long maxTime, long sinceTime) {
        XoneHttpRequest request = new XoneHttpRequest(getRequestUrl(URL_GET_PRAISE_DISCUSS_LIST),THttpMethod.GET);
        if(count > 0){
            request.addParameter("count", String.valueOf(count));
        }
        if(maxTime > 0){
            request.addParameter("maxTime", String.valueOf(maxTime));
        }
        if(sinceTime > 0){
            request.addParameter("sinceTime", String.valueOf(sinceTime));
        }
        return request;
    }

    /**
     * 获取用户发布的讨论列表
     * @param count
     * @param maxTime
     * @param sinceTime
     * @return
     */
    public THttpRequest createGetUserDiscussList(String userId, int count, long maxTime, long sinceTime) {
        XoneHttpRequest request = new XoneHttpRequest(getRequestUrl(URL_GET_USER_DISCUSS_LIST),THttpMethod.GET);
        request.addParameter("userId", userId);
        if(count > 0){
            request.addParameter("count", String.valueOf(count));
        }
        if(maxTime > 0){
            request.addParameter("maxTime", String.valueOf(maxTime));
        }
        if(sinceTime > 0){
            request.addParameter("sinceTime", String.valueOf(sinceTime));
        }
        return request;
    }

    /**
     * 获取资讯源下资讯列表
     * @return
     */
    public THttpRequest createGetSubjectInfoList(int subjectId, int currentPage ,int pageSize) {
        XoneHttpRequest request = new XoneHttpRequest(getRequestUrl(URL_GET_SUBJECT_INFO_LIST),THttpMethod.GET);
        request.addParameter("subjectId", String.valueOf(subjectId));
        if(currentPage > 0){
            request.addParameter("currentPage", String.valueOf(currentPage));
        }
        if(pageSize > 0){
            request.addParameter("pageSize", String.valueOf(pageSize));
        }
        return request;
    }

    /**
     * 获取话题下资讯列表
     * @return
     */
    public THttpRequest createGetTopicInfoList(long topicId, int currentPage ,int pageSize) {
        XoneHttpRequest request = new XoneHttpRequest(getRequestUrl(URL_GET_TOPIC_INFO_LIST),THttpMethod.GET);
        request.addParameter("topicId", String.valueOf(topicId));
        if(currentPage > 0){
            request.addParameter("currentPage", String.valueOf(currentPage));
        }
        if(pageSize > 0){
            request.addParameter("pageSize", String.valueOf(pageSize));
        }
        return request;
    }

    /**
     * 修改用户信息
     * @param user
     * @return
     */
    public THttpRequest createUpdateUserRequest(User user){
        XoneHttpRequest request = new XoneHttpRequest(getRequestUrl(URL_UPDATE_USER), THttpMethod.POST);
        List<NameValuePair> dataList = new LinkedList<NameValuePair>();
        try {
            dataList.add(new BasicNameValuePair("avatar", user.avatar));
            dataList.add(new BasicNameValuePair("nickname", user.nickname));
            dataList.add(new BasicNameValuePair("gender", String.valueOf(user.gender)));
            dataList.add(new BasicNameValuePair("birthday", String.valueOf(user.birthday)));
            dataList.add(new BasicNameValuePair("signature", user.signature));
            dataList.add(new BasicNameValuePair("province", user.province));
            dataList.add(new BasicNameValuePair("city", user.city));
            dataList.add(new BasicNameValuePair("shareLocation", String.valueOf(user.shareLocation)));
            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(dataList, "UTF-8");
            request.setHttpEntity(entity);
        } catch (Exception e) {
        }
        return request;
    }

    public THttpRequest createGetFriendDynamicListRequest(String[] ids) {
        XoneHttpRequest request = new XoneHttpRequest(getRequestUrl(URL_GET_FRIEND_DYNAMIC_LIST),
                THttpMethod.GET);
        StringBuilder sb = new StringBuilder();
        for (String id : ids) {
            if (!TextUtils.isEmpty(id))
                sb.append(id).append(",");
        }
        sb.subSequence(0, sb.length() - 1);
        request.addParameter("ids", sb.toString());
        return request;
    }

    public THttpRequest createGetDynamicIdsRequest(int allCount, int returnCount) {
        XoneHttpRequest request = new XoneHttpRequest(getRequestUrl(URL_GET_DYNAMIC_IDS),THttpMethod.GET);
        request.addParameter("count", String.valueOf(allCount));
        request.addParameter("num", String.valueOf(returnCount));
        return request;
    }

    public THttpRequest createGetAdviceListRequest() {
        XoneHttpRequest request = new XoneHttpRequest(getRequestUrl(URL_GET_ADVICE_LIST),THttpMethod.GET);
        return request;
    }

    /**
     * 反馈官方资讯
     * @param infoId
     * @param type
     * @param message
     * @return
     */
    public THttpRequest createFeedback(String infoId, int type, String message) {
        XoneHttpRequest request = new XoneHttpRequest(getRequestUrl(URL_FEED_BACK),THttpMethod.POST);
        List<NameValuePair> list = new LinkedList<NameValuePair>();

        list.add(new BasicNameValuePair("infoId", infoId));
        list.add(new BasicNameValuePair("type", String.valueOf(type)));
        list.add(new BasicNameValuePair("message", message));
        try {
            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, "UTF-8");
            request.setHttpEntity(entity);
        } catch (Exception e) {
        }

        return request;
    }

    /**
     * 举报用户讨论
     * @param infoId
     * @param type
     * @param message
     * @return
     */
    public THttpRequest createReport(String infoId, int type, String message) {
        XoneHttpRequest request = new XoneHttpRequest(getRequestUrl(URL_REPORT),THttpMethod.POST);
        List<NameValuePair> list = new LinkedList<NameValuePair>();

        list.add(new BasicNameValuePair("infoId", infoId));
        list.add(new BasicNameValuePair("type", String.valueOf(type)));
        list.add(new BasicNameValuePair("message", message));
        try {
            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, "UTF-8");
            request.setHttpEntity(entity);
        } catch (Exception e) {
        }

        return request;
    }
}
